package com.ruoyi.project.mahjong.service.impl;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.mahjong.domain.vo.OtherReceiveVo;
import com.ruoyi.project.mahjong.domain.vo.ProductRecordInfoVo;
import com.ruoyi.project.mahjong.domain.vo.RoomRecordInfoVo;
import com.ruoyi.project.mahjong.domain.vo.WorkRecordVo;
import com.ruoyi.project.mahjong.service.ITRecordService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mahjong.mapper.TWorkMapper;
import com.ruoyi.project.mahjong.domain.TWork;
import com.ruoyi.project.mahjong.service.ITWorkService;

/**
 * 交班记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-15
 */
@Service
public class TWorkServiceImpl implements ITWorkService {
    @Autowired
    private TWorkMapper tWorkMapper;
    @Autowired
    private ITRecordService recordService;

    /**
     * 查询交班记录
     *
     * @param id 交班记录ID
     * @return 交班记录
     */
    @Override
    public TWork selectTWorkById(Long id) {
        return tWorkMapper.selectTWorkById(id);
    }

    /**
     * 查询交班记录列表
     *
     * @param tWork 交班记录
     * @return 交班记录
     */
    @Override
    public List<TWork> selectTWorkList(TWork tWork) {
        return tWorkMapper.selectTWorkList(tWork);
    }

    /**
     * 新增交班记录
     *
     * @param tWork 交班记录
     * @return 结果
     */
    @Override
    public int insertTWork(TWork tWork) {
        tWork.setCreateTime(DateUtils.getNowDate());
        return tWorkMapper.insertTWork(tWork);
    }

    @Override
    public TWork insertTWorkReturnId(TWork tWork) {
        return tWorkMapper.insertTWorkReturnId(tWork);
    }

    /**
     * 修改交班记录
     *
     * @param tWork 交班记录
     * @return 结果
     */
    @Override
    public int updateTWork(TWork tWork) {
        tWork.setUpdateTime(DateUtils.getNowDate());
        return tWorkMapper.updateTWork(tWork);
    }

    /**
     * 批量删除交班记录
     *
     * @param ids 需要删除的交班记录ID
     * @return 结果
     */
    @Override
    public int deleteTWorkByIds(Long[] ids) {
        return tWorkMapper.deleteTWorkByIds(ids);
    }

    /**
     * 删除交班记录信息
     *
     * @param id 交班记录ID
     * @return 结果
     */
    @Override
    public int deleteTWorkById(Long id) {
        return tWorkMapper.deleteTWorkById(id);
    }

    @Override
    public List<WorkRecordVo> selectWorkRecordList(String startTime, String endTime, Long shopId,String staffName) {
        List<WorkRecordVo> workRecordVos = tWorkMapper.selectWorkRecordList(startTime, endTime, shopId,staffName);
        /*List<Long> ids = new ArrayList<>();
        for (WorkRecordVo vo : workRecordVos) {
            ids.add(vo.getId());
        }
//        List<WorkRecordVo> productList=recordService.selectReceive(ids);
//        商品收入及详情
        if(ids.size()!=0) {
            List<ProductRecordInfoVo> productList = recordService.selectReceive(ids);
            for (WorkRecordVo recordVo : workRecordVos) {
                Long id = recordVo.getId();
                BigDecimal jbAllMoney = new BigDecimal(0.0);
                List<ProductRecordInfoVo> productRecordInfoVos = new ArrayList<>();
                for (ProductRecordInfoVo vo : productList) {
                    if (id == vo.getJbId()) {
                        jbAllMoney = jbAllMoney.add(new BigDecimal(vo.getOneMoney())).setScale(2, BigDecimal.ROUND_HALF_UP);
                        recordVo.setProductMoney(jbAllMoney.toString());
                        productRecordInfoVos.add(vo);
                        recordVo.setProductRecordInfoVos(productRecordInfoVos);
                    }
                }
            }
        }
//        房间收入及详情
//        List<WorkRecordVo> roomReceive=recordService.selectRoomReceive(ids);
        if(ids.size()!=0) {
            List<RoomRecordInfoVo> roomReceive = recordService.selectRoomReceive(ids);
            for (WorkRecordVo recordVo : workRecordVos) {
                Long id = recordVo.getId();
                BigDecimal allMoney = new BigDecimal(0.0);
                List<RoomRecordInfoVo> voList = new ArrayList<>();
                for (RoomRecordInfoVo vo : roomReceive) {
                    if (id == vo.getJbId()) {
                        if (ObjectUtil.isNotNull(vo.getMoney())) {
                            allMoney = allMoney.add(new BigDecimal(vo.getMoney())).setScale(2, BigDecimal.ROUND_HALF_UP);
                        }
                        voList.add(vo);
                        recordVo.setRoomRecordInfoVos(voList);
                    }
                }
            }
        }
//        额外收入
        if(ids.size()!=0) {
            List<OtherReceiveVo> otherReceive = recordService.selectOtherReceive(ids);
            if (ObjectUtil.isNotEmpty(otherReceive)) {
                for (WorkRecordVo workRecordVo : workRecordVos) {
                    Long id = workRecordVo.getId();
                    BigDecimal otherReceiveMoney = new BigDecimal(0.0);
                    List<OtherReceiveVo> voList = new ArrayList<>();
                    for (OtherReceiveVo otherReceiveVo : otherReceive) {
                        if (id.equals(otherReceiveVo.getJbId())) {
                            otherReceiveMoney = otherReceiveMoney.add(new BigDecimal(otherReceiveVo.getMoney()));
                            workRecordVo.setOtherReceive(otherReceiveMoney.toString());
                            voList.add(otherReceiveVo);
                            workRecordVo.setOtherReceiveList(voList);
                        }
                    }
                }
            }
        }*/
        return workRecordVos;
    }

    @Override
    public  List<TWork> check(Long userId, Long shopId) {

        List<TWork> checkList=tWorkMapper.checkTWork(userId,shopId);
        return checkList;
    }
}
